package com.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.app.model.*;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.CategoryVo;
import com.app.vo.ProtectionPointVo;
import com.app.vo.TypeVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.ListUtils;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Controller
@RequestMapping("/api/point")
public class ProtectionPointController {
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private ProtectionPointService protectionPointService;
    @Autowired
    private PersonService personService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/getDropList")//下拉框查询
    public Result<Object> getDropList(
            HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }

        CategoryVo vo = new CategoryVo();
        List<CategoryModel> categoryModelList = categoryService.getCategoryByPid(89);
        vo.setTypeList(categoryModelList);//型号类型
        List<Integer> idList = departmentService.getDepId(user.getDepartmentId());
        List<PersonModel> personList = personService.otherPersonList(idList);
        if (personList != null && personList.size() > 0) {
            vo.setPersonList(personList);
        }
        return Result.success(vo);
    }

    @GetMapping("/getPointList")//保护点分页查询
    public Result<Object> getPointList(
            @RequestParam("page") Integer page,
            @RequestParam("limit") Integer limit,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer departmentId,
            Integer sortType,
            HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        List<Integer> idList = departmentService.getDepId(user.getDepartmentId());
        Page<ProtectionPointModel> pointModelPage = protectionPointService.getPagePointList(page, limit, name, idList,departmentId,sortType);
        for (int i = 0; i < pointModelPage.getRecords().size(); i++) {
            if (pointModelPage.getRecords().get(i).getDepartmentId()!=null){
                DepartmentModel departmentModel = departmentService.getDepByDepId((pointModelPage.getRecords().get(i).getDepartmentId()));
                pointModelPage.getRecords().get(i).setDepartmentName(departmentModel.getName());//获取部门名字
            }
        }
        if (pointModelPage.getRecords() == null) {
            return Result.error(200, "暂无数据");
        }
        int count = (int) pointModelPage.getTotal();
        List<ProtectionPointModel> pointModelList;
        pointModelList = pointModelPage.getRecords();
        ProtectionPointVo vo = new ProtectionPointVo();
        vo.setCount(count);
        vo.setList(pointModelList);
        return Result.success(vo);
    }

    @PostMapping("/insertPoint")//    新增/修改保护点
    public Result<Object> insertPoint(@RequestBody ProtectionPointModel pointModel,
                                      HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        if (pointModel.getId() > 0) {
            pointModel.setUpdateTime(CommonHelp.getNowTimestamp());
            protectionPointService.update(pointModel);
        } else {
            pointModel.setCreateTime(CommonHelp.getNowTimestamp());
            protectionPointService.insert(pointModel);
        }
        return Result.success(null);
    }

    @GetMapping("/deleteById")//删除保护点信息
    public Result<Object> deleteById(
            @RequestParam("id") Integer id,
            HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        ProtectionPointModel pointModel = protectionPointService.getPointById(id);
        if (pointModel == null) {
            return Result.error(500, "该角色不存在");
        } else {
            protectionPointService.delete(id);
            return Result.success(null);
        }
    }

    /**
     * 批量删除保护点信息
     * @param ids
     * @param request
     * @return
     */
    @DeleteMapping("/deleteBatchByIds")
    public Result<Object> deleteBatchById(@RequestParam("ids") String ids, HttpServletRequest request) {

        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        //上面token校验
      return protectionPointService.deleteBatchByIds(ids,request);
    }

}
